<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :before-close="handleClose"
    :title="title"
    width="30%">
    <dynForm
      ref="dataForm"
      :info-form="searchForm"
      :rules="rules"
      :search-tags="searchTags"
      :is-inline="false"
      :select-list="selectList"/>
    <span slot="footer">
      <el-button icon="el-icon-circle-close" @click="handleClose">{{
        $t("cancel")
      }}</el-button>
      <el-button v-btnThrottle="doSave" type="primary" icon="el-icon-receiving">{{
        $t("save")
      }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import typeManageApi from '@/api/maintenance/typeManage'
import { betterTips } from '@/utils/notification'
export default {
  name: 'TypeManageDialogForm',
  components: {},
  props: {},
  data() {
    return {
      dialogVisible: false,
      title: '',
      searchForm: { status: 1 },
      rules: {
        pmTypeName: [
          { required: true, message: `${this.$t('pleaseInput')}${this.$t('pmTypeName')}`, trigger: 'blur' }
        ],
        frequence: [
          { required: true, message: `${this.$t('pleaseInput')}${this.$t('frequence')}`, trigger: 'blur' }
        ],
        frequenceNnit: [
          { required: true, message: `${this.$t('pleaseSelect')}${this.$t('frequenceNnit')}`, trigger: 'change' }
        ]
      },
      searchTags: [
        { label: this.$t('pmTypeName'), prop: 'pmTypeName', tag: 'DynInput' },
        { label: this.$t('frequence'), prop: 'frequence', tag: 'DynInput' },
        { label: this.$t('frequenceNnit'), prop: 'frequenceNnit', tag: 'DynSelect', translate: true, style: { width: '100%' }},
        { label: this.$t('status'), prop: 'status', tag: 'DynSwitch' },
        { label: this.$t('remark'), prop: 'remark', tag: 'DynTextarea', row: 2 }
      ],
      selectList: {}
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    doSave() {
      this.$refs['dataForm'].$refs['dynForm'].validate((valid) => {
        if (valid) {
          if (this.searchForm.id) {
            typeManageApi.doUpdate(this.$clearNull(this.searchForm)).then(res => {
              betterTips(res, this.tipsTime, this.handleClose, this.handleUpdate)
            })
          } else {
            typeManageApi.doAdd(this.$clearNull(this.searchForm)).then(res => {
              betterTips(res, this.tipsTime, this.handleClose, this.handleUpdate)
            })
          }
        }
      })
    },
    handleOpen(val) {
      this.getFrequence()
      if (val && val.id) {
        this.title = this.$t('modify')
        this.searchForm = Object.assign({}, val)
        this.$set(this.searchTags, 0, { label: this.$t('pmTypeName'), prop: 'pmTypeName', tag: 'text', disabled: true })
      } else {
        this.title = this.$t('add')
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].$refs['dynForm'].clearValidate()
      })
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    // resetForm(formName) {
    //   if (this.searchForm.id) return
    //   this.searchForm = this.$options.data.call(this).searchForm
    // },
    // 触发父组件更新
    handleUpdate(formName) {
      this.$emit('initList')
    },
    // 获取频率下拉框
    async getFrequence() {
      const res = await typeManageApi.getFrequence()
      this.$set(this.selectList, 'frequenceNnit', res.data)
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/defaultStyle.scss";
</style>
